<template>
	<view>
		<!-- 输入员工信息 -->
		<view class="yuangong flex justify-center">
			<view class="yuangong-box flex justify-center">
				<view class="yuangong-box-c">
					<!-- <view class="yuangong-box-c-item flex justify-between align-center">
						<text>员工名称</text>
						<view class="" style="width: 70%;">
							<u-input v-model="name" input-align="right" placeholder="请输入员工名称" type="text"
								:border="false" />
						</view>
					</view> -->
					<view class="yuangong-box-c-item flex justify-between align-center">
						<text>联系电话</text>
						<view class="" style="width: 70%;">
							<u-input v-model="phone" input-align="right" maxlength="11" placeholder="请输入联系方式" type="number"
								:border="false" />
						</view>
					</view>
					<view class="yuangong-box-c-item flex justify-between align-center" style="border: none;">
						<text>短信验证码</text>
						<view class="flex align-center" style="width: 70%;">
							<u-input v-model="code" input-align="right" placeholder="请输入短信验证码" type="text"
								:border="false" />
							<u-toast ref="uToast"></u-toast>
							<u-verification-code :seconds="seconds" @end="end" @start="start" ref="uCode"
								@change="codeChange"></u-verification-code>
							<view class="code flex justify-center align-center" @tap="getCode">
								{{tips}}
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="add flex justify-center">
			<view class="add-box flex justify-center align-center" @click="add()">
				确认添加
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				code:'',
				name: '',
				phone: '',
				tips: '',
				// refCode: null,
				seconds: 59,
			};
		},
		methods: {
			add(){
				if(this.phone==''){
					uni.showToast({
						title:'请输入手机号',
						icon:'none'
					})
					return
				}else if(this.code==''){
					uni.showToast({
						title:'请输入验证码',
						icon:'none'
					})
					return
				}
				
				
				let data = {
					phone:this.phone,
					msg:this.code
				}
				this.$Request.postT('/app/user/insertStaffUser',data).then(res => {
					if (res.code == 0) {
						uni.showToast({
							title:'添加成功'
						})
						setTimeout(()=>{
							uni.navigateBack()
						},1500)
					}else{
						uni.showToast({
							title:res.msg,
							icon:'none'
						})
					}
				})
			},
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					let reg = /^1[3-9]\d{9}$/
					if(this.phone==''){
						uni.showToast({
							title:'请输入手机号',
							icon:'none'
						})
						return
					}
					if(!reg.test(this.phone)){
						uni.showToast({
							title:'请输入正确的手机号',
							icon:'none'
						})
						return
					}
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					this.$Request.getT('/app/Login/sendMsg/'+this.phone+'/bingStaffUser').then(res => {
						if (res.code == 0) {
							uni.hideLoading();
							// 这里此提示会被this.start()方法中的提示覆盖
							this.$u.toast('验证码已发送');
							// 通知验证码组件内部开始倒计时
							this.$refs.uCode.start();
						}else{
							uni.showToast({
								title:res.msg,
								icon:'none'
							})
						}
					})
				} else {
					this.$u.toast('倒计时结束后再发送');
				}
			},
			end() {
				// this.$u.toast('倒计时结束');
			},
			start() {
				// this.$u.toast('倒计时开始');
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background: #F5F5F5;
	}
	.code{
		width: 160rpx;
		padding: 10rpx 0 10rpx 0;
		border-radius: 25rpx;
		font-size: 20rpx;
		border: 2rpx solid #999999;
		margin-left: 20rpx;
	}
	.wrap {
		padding: 24rpx;
	}

	.yuangong {
		width: 100%;
		height: 260rpx;
		margin-top: 30rpx;

		.yuangong-box {
			width: 686rpx;
			height: 100%;
			background: #FFFFFF;
			border-radius: 24rpx;

			.yuangong-box-c {
				width: 626rpx;
				height: 100%;
			}

			.yuangong-box-c-item {
				width: 100%;
				height: 108rpx;
				border-bottom: 1rpx solid #F2F2F2;
				text{
					color: #0D0D0D;
					font-size: 30rpx;
					font-weight: bold;
				}
			}
		}
	}
	.add{
		width: 100%;
		height: 100rpx;
		margin-top: 20rpx;
		.add-box{
			width: 686rpx;
			height: 100rpx;
			border-radius: 12rpx;
			background-color: #557EFD;
			color: #FFFFFF;
			font-size: 32rpx;
			font-weight: 800;
		}
	}
</style>
